package com.eascs.workflow.wf.dubbo;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.eascs.app.dao.PageResult;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.workflow.common.entity.wf.DicData;
import com.eascs.workflow.common.entity.wf.ProcessTaskMain;
import com.eascs.workflow.common.entity.wf.ProcessTaskNode;
import com.eascs.workflow.common.service.wf.WorkFlowService;
import com.eascs.workflow.common.vo.CancleProcessParam;
import com.eascs.workflow.common.vo.CloseProcessParam;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.common.vo.ProcessTimeParam;
import com.eascs.workflow.common.vo.ProcessTimeVo;
import com.eascs.workflow.common.vo.UserTaskParam;
import com.eascs.workflow.common.vo.UserTaskVo;
import com.eascs.workflow.common.vo.WFRequestParam;
import com.eascs.workflow.email.service.WFEmailService;
import com.eascs.workflow.wf.entity.WorkWarnEntity;
import com.eascs.workflow.wf.service.TemplateService;
import com.eascs.workflow.wf.service.WFProcessService;
import com.eascs.workflow.wf.service.WFQueryService;

@Service
public class WorkFlowServiceImpl implements WorkFlowService {
	private static Logger log = LoggerFactory.getLogger(WorkFlowServiceImpl.class);

	@Autowired
	private WFProcessService wFService;

	@Autowired
	private WFQueryService wFQueryService;

	@Autowired
	private WFEmailService wfEmailService;
	@Autowired
	private TemplateService templateService;

	@Override
	public ModelResult<Boolean> instance(WFRequestParam requestParam) {
		try {
			ModelResult<ProcessTaskMain> modelResult = wFService.instance(requestParam);
			if (modelResult.isSuccess()) {
				return new ModelResult<Boolean>().withModel(Boolean.TRUE);
			}
			return new ModelResult<Boolean>().withError(modelResult.getErrCode(), modelResult.getErrMsg());
		} catch (Exception e) {
			log.error("流程实例化失败," + requestParam, e);
			ModelResult<Boolean> modelResult = new ModelResult<Boolean>("process.instance.fail", "流程实例化失败");
			modelResult.withStackTrace(e);
			return modelResult;
		}
	}

	@Override
	public ModelResult<ProcessTaskMain> instanceWithResult(WFRequestParam requestParam) {
		try {
			return wFService.instance(requestParam);
		} catch (Exception e) {
			log.error("流程实例化失败," + requestParam, e);
			ModelResult<ProcessTaskMain> modelResult = new ModelResult<ProcessTaskMain>("process.instance.fail",
					"流程实例化失败");
			modelResult.withStackTrace(e);
			return modelResult;
		}
	}

	public ModelResult<PageResult<UserTaskVo>> queryUserTaskByPage(UserTaskParam userTaskParam) {
		return wFQueryService.queryUserTaskByPage(userTaskParam);
	}

	@Override
	public ModelResult<ProcessTaskMain> queryTaskMainById(String taskId) {
		return wFQueryService.queryTaskMainById(taskId);
	}

	@Override
	public ModelResult<Boolean> closeProcess(CloseProcessParam closeProcessParam) {
		return wFService.closeProcess(closeProcessParam);
	}

	@Override
	public ModelResult<String> queryReferIdByNodeAuditorId(String nodeAuditorId) {
		return wFQueryService.queryReferIdByNodeAuditorId(nodeAuditorId);
	}

	@Override
	public ModelResult<PageResult<ProcessTimeVo>> queryProcessTimes(ProcessTimeParam param) {
		try {
			return wFQueryService.queryProcessTimes(param);
		} catch (Exception e) {
			log.error("查询流程审批时间失败", e);
			ModelResult<PageResult<ProcessTimeVo>> modelResult = new ModelResult<>(
					"WorkFlowServiceImpl.queryProcessTimes", "查询流程审批时间失败");
			modelResult.withStackTrace(e);
			return modelResult;
		}
	}

	@Override
	public ModelResult<Boolean> wfWorkWarnEmail() {
		log.info("wfWorkWarn.email.save.start...");
		ModelResult<Boolean> modelResult = new ModelResult<Boolean>();
		try {
			Map<String, List<WorkWarnEntity>> map = wFQueryService.queryWorkWarnList();
			log.info("wfWorkWarn.query.map size[{}]", map.size());
			for (String key : map.keySet()) {
				wfEmailService.WorkWarnEmail(map.get(key));
			}
		} catch (Exception e) {
			log.error("待办提醒保存失败,", e);
			modelResult.setModel(false);
			modelResult.withStackTrace(e);
			return modelResult;
		}
		log.info("wfWorkWarn.email.save.end...");
		modelResult.setModel(true);
		return modelResult;
	}

	@Override
	public ModelResult<String> getProvincebyUNITNO(String unitno) {
		return wFQueryService.getProvincebyUNITNO(unitno);
	}

	@Override
	public ModelResult<List<Object[]>> getProcessTaskStatus(List<String> taskIds) {
		return null;
	}

	@Override
	public ModelResult<List<DicData>> getProcessTypeByCatagory(String catagory) {
		return templateService.getProcessTypeByCatagory(catagory);
	}

	@Override
	public ModelResult<Boolean> cancelProcess(CancleProcessParam param) {
		try {
			return wFService.cancelProcess(param);
		} catch (Exception e) {
			log.error("流程作废失败," + param, e);
			return new ModelResult<Boolean>().withError("流程作废失败", "流程作废失败");
		}
	}

	@Override
	public ModelResult<ProcessTaskMain> getAgreeInfo(String referId, String processType) {
		return wFQueryService.queryAgreeDetail(referId, processType);
	}

	@Override
	public ModelResult<List<ProcessTaskMain>> queryAgreeList(String referId) {
		return wFQueryService.queryAgreeList(referId);
	}

	@Override
	public ProcessTaskNode getTaskNodeDetail(String referId, String processType, String nodeCode) {
		return wFQueryService.getTaskNodeDetail(referId, processType, nodeCode);
	}
}
